package com.yun.zf.admin.controller;

import com.yun.zf.common.admin.model.sys.RoleAddMo;
import com.yun.zf.common.admin.model.sys.RoleSearchMo;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.exception.ExceptionUtil;
import com.yun.zf.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色信息 ,角色管理只能管理员和开发人员使用，生产环境不能配置开发人员角色
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/role")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @PostMapping("/list")
    @ApiOperation(value = "角色列表")
    public Response list(@RequestBody RoleSearchMo role)
    {
        return Response.success(roleService.selectRoleList(role));
    }


    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @GetMapping(value = "/{roleId}")
    @ApiOperation(value = "角色详情")
    public Response getInfo(@PathVariable Long roleId)
    {
        return Response.success(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public Response add(@Validated @RequestBody RoleAddMo role)
    {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role.getRoleId(),role.getRoleName())))
        {
            return Response.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role.getRoleId(),role.getRoleKey())))
        {
            return Response.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        return Response.success(roleService.insertRole(role));

    }

    /**
     * 修改保存角色,生产只有管理员可以配置修改角色
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @PostMapping("/edit")
    @ApiOperation(value = "修改角色")
    public Response edit(@Validated @RequestBody RoleAddMo role)
    {
           // roleService.checkRoleAllowed(role.getRoleId());

        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role.getRoleId(),role.getRoleName())))
        {
            return Response.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role.getRoleId(),role.getRoleKey())))
        {
            return Response.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        return Response.success(roleService.updateRole(role));
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @PutMapping("/dataScope")
    @ApiOperation(value = "修改数据权限--废弃")
    public Response dataScope(Long roleId,String dataScope,Long[] deptIds)
    {
        roleService.checkRoleAllowed(roleId);
        return Response.success(roleService.authDataScope(roleId,dataScope,deptIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,develop')")
    @PutMapping("/changeStatus")
    @ApiOperation(value = "修改状态")
    public Response changeStatus(Long roleId,String status)
    {
        roleService.checkRoleAllowed(roleId);
      int cn =  roleService.checkDelFlag(roleId);
      if (cn==0){
        return   Response.error("角色已经删除，不允许在修改状态");
      }
        return Response.success(roleService.updateRoleStatus(roleId,status));
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping("/remove")
    @ApiOperation(value = "删除角色")
    public Response remove(@RequestBody Long[] roleIds)
    {
        try {
            roleService.deleteRoleByIds(roleIds);
        }catch (Exception e){
            return Response.error(ExceptionUtil.getExceptionMessage(e));
        }

        return Response.success();
    }


}